package com.kk.work04.controller;

import com.kk.work04.entity.Books;
import com.kk.work04.service.BooksService;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * (Books)表控制层
 *
 * @author makejava
 * @since 2025-04-30 19:39:12
 */
@RestController
@RequestMapping("books")
public class BooksController {
    /**
     * 服务对象
     */
    @Resource
    private BooksService booksService;


    /**
     * 分页查询
     *
     * @param books       筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Books>> queryByPage(Books books, PageRequest pageRequest) {
        return ResponseEntity.ok(this.booksService.queryByPage(books, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Books> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.booksService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param books 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Books> add(Books books) {
        return ResponseEntity.ok(this.booksService.insert(books));
    }

    @PostMapping("file")
    public Integer addFile(@RequestParam("file") MultipartFile file) {
      int i =   booksService.addFile(file);
      return i;
    }
    /**
     * 编辑数据
     *
     * @param books 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Books> edit(Books books) {
        return ResponseEntity.ok(this.booksService.update(books));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.booksService.deleteById(id));
    }

}

